package com.github.config;

import com.github.interceptor.AuthHandshakeInterceptor;
import com.github.websocket.WSHandler;
import com.github.websocket.Handshake;
import com.github.websocket.UserStatusWebSocketHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {

    @Autowired
    @Qualifier("wsHandler")
    private WSHandler wsHandler;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    @Qualifier("userStatusWebSocketHandler")
    private UserStatusWebSocketHandler userStatusWebSocketHandler;


    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        // 注册WebSocket处理器，并指定WebSocket的路径
        registry.addHandler(wsHandler, "/websocket")
                .addInterceptors(new Handshake(stringRedisTemplate)).setAllowedOrigins("*");

        registry.addHandler(userStatusWebSocketHandler, "/ws/user-status")
                .addInterceptors(new AuthHandshakeInterceptor()).setAllowedOrigins("*");
    }
}
